const { cloud,trackMoreFun} = getApp();
Page({
  data: {
    code: '', // 根据标记来判断是那个h5渲染出来
    showWebView: false, // 展示webview
    webViewSrc: '', // h5链接
    webViewContext: null, // web-view 上下文 webViewContext 对象
    shareData: {}, // 分享的数据
  },
  async onLoad(query) {
    // 可以在增加编译模式的时候加上参数
    // query.code = 'leleftpro';

    // 时间戳
    const time = new Date().getTime();
    // 隐藏返回到首页的按钮
    my.hideBackHome({});
    console.log('page onLoad', query);
    // 获取参数
    const code = query && query.code || 'fathersday'; // 默认为父亲节项目
    const channel = query && query.channel ||'miniapp'; // 默认为父亲节项目
    console.log(channel,'channelchannel')
    getApp().globalData.channel2 = channel;
    // 获取设备信息
    const { device = '' } = await this.getSystemInfo();
    // console.log(new Date().getTime() - time)

    // 根据 code 设计标题和 webview 地址
    let webViewSrc = '';
    let pageTitle = '';

    // 香奈儿父亲节
    if (code === 'fathersday') {
      webViewSrc = `https://chanel-n5-drop.morefun.me/fathersday/index.html?time=${time}&channel=${channel}&device=${device}`;
      pageTitle = '香奈儿父亲节礼选';
    }
    // 香奈儿父亲节
    if (code === 'ultra_le_teint') {
      webViewSrc = `https://chanel-n5-drop.morefun.me/ultra_le_teint/index.html?time=${time}&channel=${channel}&device=${device}`;
      pageTitle = '香奈儿柔光持妆系列';
    }
    // 其他项目
    if (code === 'leleftpro') {
      webViewSrc = `https://chanel-n5-drop.morefun.me/leleftpro/index.html?time=${time}&channel=${channel}&device=${device}`;
      pageTitle = '香奈儿多维紧肤系列';
    }
    if (code === 'personality_symbol') {
      webViewSrc = `https://chanel-n5-drop.morefun.me/personality_symbol/index.html?time=${time}&channel=${channel}&device=${device}`;
      pageTitle = '香奈儿的个性象征';
    }

    if (code === 'personality_symbol_text') {
      webViewSrc = `https://chanel-n5-drop.morefun.me/personality_symbol_test/index.html?time=${time}&channel=${channel}&device=${device}`;
      pageTitle = '香奈儿的个性象征';
    }

    // 参数重新赋值
    if (query && query.webviewsrc) webViewSrc = decodeURIComponent(query.webviewsrc);
    if (query && query.pagetitle) pageTitle = decodeURIComponent(query.pagetitle);

    if (webViewSrc && pageTitle) {
      // 设置标题
      my.setNavigationBar({ title: pageTitle });
      // 获取用户信息
      const { openId = '1234' } = await this.getUserInfo();
      // my.showToast({ content: '设备' + (new Date().getTime() - time) });

      this.setData({
        showWebView: true,
        webViewSrc: webViewSrc + '&openId=' + openId,
        code,
      })
      // 初始话
      this.webViewContext = my.createWebViewContext("web-view-h5");
      // my.showToast({ content:'加载时间:' + (new Date().getTime() - time)  });
    }
  },
  async onShow() {
    await this.postMessage({ action: 'onShow' });
  },
  async onHide() {
    await this.postMessage({ action: 'onHide' });
  },
  // 侦听h5的事件
  async onMessage(e) {
    const { action = '', param = {} } = e.detail;
    if (action) {
      // 提示
      if (action == 'showToast') {
        const { content = '弱提示' } = param;
        my.showToast({ content });
      }
      // 加购
      if (action == 'showSku') {
        const { itemId = '', skuId = '' } = param;
        if (itemId) my.tb.showSku({ itemId, skuId });
      }
      // 跳转自定义链接
      if (action == 'navigateToOutside') {
        const { url = '' } = param;
        if (url) my.call("navigateToOutside", { url });
      }
      // 去商品详情
      if (action == 'goPdp') {
        const { itemId = '', skuId = '' } = param;
        if (itemId) my.tb.openDetail({ itemId, skuId });
      }
      // 更新背景颜色
      if (action == 'changeBgColor') {
        const { color = '#FFFFFF' } = param;
        this.setData({
          bgColor: color
        })
      }

      if (action == 'navigate') {
        const { url } = param;
        my.navigateTo({ url: url })
      }
    }
  },
  // 分享
  onShareAppMessage() {
    if (this.data.code === 'fathersday') {
      return {
        title: '香奈儿父亲节礼选', // 标题
        desc: '爱自心深处', // 描述
        path: '/pages/webview/webview?code=fathersday', // 页面
        imageUrl: 'https://morefun-active.oss-cn-beijing.aliyuncs.com/morefun2024/chanel/fathersday/share.jpg'
      }
    } else if (this.data.code === 'ultra_le_teint') {
      return {
        title: '香奈儿柔光持妆系列', // 标题
        desc: '柔亮哑光 实力持妆', // 描述
        path: '/pages/webview/webview?code=ultra_le_teint', // 页面
        imageUrl: 'https://morefun-active.oss-cn-beijing.aliyuncs.com/morefun2024/chanel/ultra_Le_Teint/img/share/share.png'
      }
    } else if (this.data.code === 'leleftpro') {
      return {
        title: '香奈儿多维紧肤系列', // 标题
        desc: '构筑轮廓立体之美', // 描述
        path: 'pages/webview/webview?code=leleftpro', // 页面
        imageUrl: 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/202404ChanelPro/shareImg.jpg'
      }
    } else if (this.data.code === 'personality_symbol') {
      return {
        title: '香奈儿的个性象征', // 标题
        desc: '香奈儿的个性象征', // 描述
        path: 'pages/webview/webview?code=personality_symbol', // 页面
        imageUrl: 'https://morefun-active.oss-cn-beijing.aliyuncs.com/chanel_c2b_web/xing/longpage/share.jpg'
      }
    } else {
      // 如果没有设置就不让分享
      return {};
    }
  },
  // 获取用户信息
  async getUserInfo() {
    // 在本地缓存中获取,直接返回
    let openId = '';
    const userInfo = my.getStorageSync({ key: 'webview-openId' });
    if (userInfo && userInfo.data) openId = userInfo.data;
    if (!openId) {
      const res = await cloud.function.invoke('fathersday');
      if (res && res.success && res.data && res.data.openId) {
        openId = res.data.openId;
        my.setStorageSync({
          key: 'webview-openId',
          data: openId
        });
      }
    }
    return { openId };
  },
  // 获取设备信息
  async getSystemInfo() {
    // 在本地缓存中获取,直接返回
    let device = '';
    const deviceInfo = my.getStorageSync({ key: 'webview-device' });
    if (deviceInfo && deviceInfo.data) device = deviceInfo.data;
    if (!device) {
      const { platform = '', brand = '', model = '', statusBarHeight = '' } = my.getSystemInfoSync();
      device = platform + '_' + brand + '_' + model + '_' + statusBarHeight;
    }

    return { device };
  },
  // 向H5发送消息
  async postMessage(obj = {}) {
    if (this.webViewContext) {
      try {
        this.webViewContext.postMessage(obj);
      } catch (e) {
        console.log('postMessage事件异常:' + JSON.stringify(e))
      }
    }
  },
});